package com.easylive.utils;

import com.easylive.entity.config.AppConfig;
import com.easylive.entity.constants.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;

@Component
public class FFmpegUtils {
    @Resource
    private AppConfig appConfig;

    public void createImageThumbnail(String filePath){
        String CMD = "ffmpeg -i \"%s\" -vf scale=200:-1 \"%s\"";
        CMD = String.format(CMD,filePath,filePath + Constants.IMAGE_THUMBNAIL_SUFFIX);
        ProcessUtils.executeCommand(CMD,true);
    }

    public Integer getVideoInfoDuration(String completeVideo){
        final String CMD_GET_CODE = "ffprobe -show_entries format=duration -v error -select_streams v:0 \"%s\"";
        String CMD = String.format(CMD_GET_CODE,completeVideo);
        String result = ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());

        if(StringTools.isEmpty(result)){
            return 0;
        }

        result = result.replace("\n", "");
        result = result.replace("[FORMAT]duration=", "");
        result = result.replace("[/FORMAT]", "");

        return new BigDecimal(result).intValue();
    }

    public String getVideoCodec(String videoFilePath){
        final String CMD_GET_CODE = "ffprobe -v error -select_streams v:0 -show_entries stream=codec_name \"%s\"";
        String CMD = String.format(CMD_GET_CODE,videoFilePath);
        String result = ProcessUtils.executeCommand(CMD,appConfig.getShowFFmpegLog());
        result = result.replace("\n", "");
        result = result.substring(result.indexOf("=") + 1);
        String codec = result.substring(0, result.indexOf("["));
        return codec;
    }
    public void convertHevc2Mp4(String tempFileName,String videoFilePath){
        String CMD_HEVC_264 = "ffmpeg -i \"%s\" -c:v libx264 -crf 20 \"%s\" -y";
        String cmd = String.format(CMD_HEVC_264,tempFileName,videoFilePath);
        ProcessUtils.executeCommand(cmd,true);
    }

    public void convertVideo2Ts(File tsfolder, String videoFilePath) {
        final String CMD_TRANSFER_2TS = "ffmpeg -i \"%s\" -codec copy -bsf:v h264_mp4toannexb \"%s\"";
        final String CMD_CUT_TS = "ffmpeg -i \"%s\" -c copy -map 0 -f segment -segment_list \"%s\" -segment_time 10 %s/%%4d.ts";
        String tsPath = tsfolder + "/" + Constants.TS_NAME;
        //生成TS
        String cmd = String.format(CMD_TRANSFER_2TS,videoFilePath,tsPath);
        ProcessUtils.executeCommand(cmd,true);

        cmd = String.format(CMD_CUT_TS,tsPath,tsfolder.getPath() + "/" + Constants.M3U8_NAME,tsfolder.getPath());
        ProcessUtils.executeCommand(cmd,true);
        new File(tsPath).delete();



    }




    public static void main(String[] args) {
        final String CMD_GET_CODE = "ffprobe -show_entries format=duration -v error -select_streams v:0 \"%s\"";
        String completeVideo = "C:\\Users\\86150\\Videos\\1月10日 (1)(1).mp4";
        String CMD = String.format(CMD_GET_CODE,completeVideo);
        String result = ProcessUtils.executeCommand(CMD,true);
        result = result.replace("\n", "");
        result = result.replace("[FORMAT]duration=", "");
        result = result.replace("[/FORMAT]", "");
        String a = result;
        System.out.println(a);
    }
}
